/**
 * 访问 "设备"接口网络模块
 */
 
import axios from "./axios.config";
import util from "./util";
 
class DeviceApi {
  async findAll(limit, offset, orderBy, sort) {
    let result = await axios.get(`/device?limit=${limit}&offset=${offset}&order_by=${orderBy}&sort=${sort}`);
    return util.jsonTransfer(result.data);
  }
  async getCount(){
    let result = await axios.get(`/device/count`);
    return util.jsonTransfer(result.data);
  }
    async find(deviceId){
      let result = await axios.get(`/device/${deviceId}`);
      return util.jsonTransfer(result.data);
    }
    async fetchXiamen(xiamenId){
      let result = await axios.get(`/device/fetch/xiamen/${xiamenId}`);
      return util.jsonTransfer(result.data);
    }
 
    async add(device){
      let result = await axios.post(`/device`, device);
      return util.jsonTransfer(result.data);
    }
    async edit(device){
      let result = await axios.put(`/device/${device.id}`, device);
      return util.jsonTransfer(result.data);
    }
    async delete(deviceId){
      let result = await axios.delete(`/device/${deviceId}`);
      return util.jsonTransfer(result.data);
    }
    async findByXiamenId(xiamenId){
      let result = await axios.get(`/device/id/${xiamenId}`);
      return util.jsonTransfer(result.data);
    }
    
    async search(query, limit = 10, offset = 0) {
      let result = await axios.get(`/device/search?q=${query}&limit=${limit}&offset=${offset}`);
      return util.jsonTransfer(result.data);
    }

    async createBooking(bookingData) {
      let result = await axios.post('/deuse/borrows', bookingData);
      return util.jsonTransfer(result.data);
    }

    async getBookings(deviceId) {
      let result = await axios.get(`/deuse/borrows?deviceId=${deviceId}`);
      return util.jsonTransfer(result.data);
    }

    async getUserBookings() {
      let result = await axios.get('/deuse/borrows');
      return util.jsonTransfer(result.data);
    }

    async cancelBooking(bookingId) {
      let result = await axios.post('/deuse/borrows/cancel', { bookingId });
      return util.jsonTransfer(result.data);
    }

    /**
     * 获取预约记录列表
     * @param {Object} params 查询参数
     * @returns {Promise} 预约记录列表
     */
    async getBookingRecords(params = {}) {
      try {
        const queryString = new URLSearchParams(params).toString()
        const response = await axios.get(`/deuse/borrows?${queryString}`)
        return response.data
      } catch (error) {
        console.error('获取预约记录失败:', error)
        throw error
      }
    }

    /**
     * 更新预约状态
     * @param {Object} data 更新数据
     * @returns {Promise} 更新结果
     */
    async updateBookingStatus(data) {
      try {
        const response = await axios.put('/deuse/borrows/status', data)
        return util.jsonTransfer(response.data)
      } catch (error) {
        console.error('更新预约状态失败:', error)
        throw error
      }
    }

    /**
     * 归还设备
     * @param {Object} data 归还数据
     * @returns {Promise} 归还结果
     */
    async returnDevice(data) {
      try {
        const response = await axios.post('/deuse/borrows/return', data);
        return util.jsonTransfer(response.data);
      } catch (error) {
        console.error('归还设备失败:', error);
        throw error;
      }
    }

    /**
     * 获取设备使用记录
     * @param {number} deviceId 
     * @param {number} limit 
     * @param {number} offset 
     */
    async getUsageRecords(deviceId, limit = -1, offset = -1) {
      try {
        const response = await axios.get(`/deuse/device/${deviceId}/usage-records`, {
          params: { limit, offset }
        });
        return util.jsonTransfer(response.data);
      } catch (error) {
        console.error('获取使用记录失败:', error);
        throw error;
      }
    }

    /**
     * 获取超期未归还的设备列表
     * @returns {Promise<Object>} 超期设备列表
     */
    async getOverdueDevices() {
      try {
        const response = await axios.get('/deuse/borrows/overdue');
        return response.data;
      } catch (error) {
        console.error('获取超期设备失败:', error);
        throw error;
      }
    }

    /**
     * 添加设备使用记录
     * @param {number} deviceId 
     * @param {string} usageNote 
     */
    async addUsageRecord(deviceId, usageNote) {
      try {
        const response = await axios.post(`/deuse/device/${deviceId}/usage-records`, {
          usageNote
        });
        return util.jsonTransfer(response.data);
      } catch (error) {
        console.error('添加使用记录失败:', error);
        throw error;
      }
    }

    /**
     * 获取设备评价列表
     * @param {number} deviceId - 设备ID
     * @returns {Promise<Object>} 评价列表
     */
    async getDeviceFeedback(deviceId) {
      return axios.get(`/deviceFeedback/${deviceId}/feedback`);
    }

    /**
     * 获取设备平均评分
     * @param {number} deviceId - 设备ID
     * @returns {Promise<Object>} 平均评分和总评分数
     */
    async getDeviceRating(deviceId) {
      return axios.get(`/deviceFeedback/${deviceId}/rating`);
    }

    /**
     * 添加设备评价
     * @param {number} deviceId - 设备ID
     * @param {Object} feedback - 评价信息
     * @param {number} feedback.rating - 评分
     * @param {string} feedback.feedback - 评价内容
     * @returns {Promise<Object>} 评价结果
     */
    async addDeviceFeedback(deviceId, feedback) {
      try {
        const response = await axios.post(`/deviceFeedback/${deviceId}/feedback`, feedback);
        return util.jsonTransfer(response.data);
      } catch (error) {
        console.error('添加设备评价失败:', error);
        throw error;
      }
    }

    /**
     * 点赞评价
     * @param {number} feedbackId - 评价ID
     * @returns {Promise<Object>} 点赞结果
     */
    async likeFeedback(feedbackId) {
      return axios.post(`/deviceFeedback/${feedbackId}/like`);
    }

    /**
     * 踩评价
     * @param {number} feedbackId - 评价ID
     * @returns {Promise<Object>} 踩结果
     */
    async dislikeFeedback(feedbackId) {
      return axios.post(`/deviceFeedback/${feedbackId}/dislike`);
    }

    /**
     * 设置设备状态
     * @param {number} deviceId - 设备ID
     * @param {string} status - 设备状态 (e.g., 'available', 'maintenance', 'deactivated')
     * @returns {Promise<Object>} 更新结果
     */
    async setDeviceStatus(deviceId, status) {
      try {
        const response = await axios.put(`/device/${deviceId}/status`, { status });
        return util.jsonTransfer(response.data);
      } catch (error) {
        console.error('设置设备状态失败:', error);
        throw error;
      }
    }
}
 
export default DeviceApi;